#include <Wire.h>               // Only needed for Arduino 1.6.5 and earlier
#include "SSD1306Wire.h"        // legacy: #include "SSD1306.h"
SSD1306Wire display(0x3c, 15, 2);   // ADDRESS, SDA, SCL  -  SDA and SCL usually populate automatically based on your board's pins_arduino.h e.g. https://github.com/esp8266/Arduino/blob/master/variants/nodemcu/pins_arduino.h

#define DEMO_DURATION 3000
typedef void (*Demo)(void);

int demoMode = 0;
int counter = 1;

#include <btAudio.h>

// Sets the name of the audio device
btAudio audio = btAudio("z_yy");

void setup() {
  Serial.begin(115200);
  Serial.println();

  // Initialising the UI will init the display too.
  display.init();

  display.flipScreenVertically();
  display.setFont(ArialMT_Plain_10);

  for(int i = 0 ; i < 99 ;i ++){
    delay(10);
    counter++;
    drawProgressBarDemo();
  }

  
  audio.begin();
  int bck = 14; 
  int ws = 13;
  int dout = 12;
  audio.I2S(bck, dout, ws);
}

void drawFontFaceDemo() {
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.setFont(ArialMT_Plain_10);

  Serial.print("Title: ");
  Serial.println(audio.title);
  Serial.print("Artist: ");
  Serial.println(audio.artist);
  Serial.print("Album: ");
  Serial.println(audio.album);
  Serial.print("Genre: ");
  Serial.println(audio.genre);

  display.drawString(0, 0, audio.title);
  display.setFont(ArialMT_Plain_16);
  display.drawString(0, 10, audio.artist);
  display.setFont(ArialMT_Plain_24);
  display.drawString(0, 26, audio.album);
}

void drawTextFlowDemo() {
  display.setFont(ArialMT_Plain_10);
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.drawStringMaxWidth(0, 0, 128,
                             "Lorem ipsum\n dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore." );
}

void drawTextAlignmentDemo() {
  // Text alignment demo
  display.setFont(ArialMT_Plain_10);

  // The coordinates define the left starting point of the text
  display.setTextAlignment(TEXT_ALIGN_LEFT);
  display.drawString(0, 10, "Left aligned (0,10)");

  // The coordinates define the center of the text
  display.setTextAlignment(TEXT_ALIGN_CENTER);
  display.drawString(64, 22, "Center aligned (64,22)");

  // The coordinates define the right end of the text
  display.setTextAlignment(TEXT_ALIGN_RIGHT);
  display.drawString(128, 33, "Right aligned (128,33)");
}

void drawProgressBarDemo() {
  display.clear();
  int progress = (counter / 1) % 100;
  // draw the progress bar
  display.drawProgressBar(0, 32, 120, 10, progress);

  // draw the percentage as String
  display.setTextAlignment(TEXT_ALIGN_CENTER);
  display.drawString(64, 15, String(progress) + "%");

  // draw the current demo method
  display.display();
}

void drawCircleDemo() {
  for (int i = 1; i < 8; i++) {
    display.setColor(WHITE);
    display.drawCircle(32, 32, i * 3);
    if (i % 2 == 0) {
      display.setColor(BLACK);
    }
    display.fillCircle(96, 32, 32 - i * 3);
  }
}

void loop() {
    // updates metadata
  audio.updateMeta();
  // clear the display
  display.clear();
  // draw the current demo method

  display.setFont(ArialMT_Plain_10);
  display.setTextAlignment(TEXT_ALIGN_RIGHT);
  display.drawString(128, 54, String(millis()));

  drawFontFaceDemo();
  // write the buffer to the display
  display.display();

  delay(1000);
}
